package mop.controller;

import java.util.List;

import mop.domain.project.*;
import mop.exception.*;

/**
 * The connection between GUI and domain.
 * Only knows built-in Java data types and strings.
 * 
 * @invar	Has no variables of its own, except for pool, 
 * 			which is a reference to a static variable.
 *  
 * @author	MOP Team 7
 */
public class ProjectController 
{

	/**
	 * Creates a ProjectController with no extra settings.
	 * Constructor must be at protected level.
	 * Allows inheritance, but no out of package creation.
	 * 
	 * @post 	pool is set to ProjectPool.getInstance().
	 */
	protected ProjectController()
	{
		this.pool = ProjectPool.getInstance();
	}
	
	
	/***************************************************************************
	 * 							instance
	 ***************************************************************************/
	
	/**
	 * Use this function to access ProjectController always
	 * 
	 * @return 	The static instance of ProjectController.
	 */
	public static ProjectController getInstance()
	{
		if(instance == null) 
		{
	         instance = new ProjectController();
	    }
		return instance;	
	}
	
	/**
	 * Instance to self.
	 */
	private static ProjectController instance = null;
	
	
	/***************************************************************************
	 * 							ProjectPool
	 ***************************************************************************/
	
	/**
	 * Returns a list of all the projects.
	 * 
	 * @return	A list of project names from the pool (String objects).
	 */
	public List<String> getProjectList()
	{
		return this.pool.getProjectList();
	}
	
	/**
	 * Creates a new project.
	 * 
	 * @param 	description
	 *  		The description of the project.
	 * 
	 * @post 	A new project instance is created with name and description in the pool.
	 * 
	 * from our lovely pool it throws :
	 * @throws 	IllegalDuplicateException
	 * 			if the project is a duplicate
	 * 			| !isUniqueProject(description)
	 *       
	 * @throws 	IllegalDescriptionException
	 * 			if the description isn't a non-empty string
	 * 			| !isValidDescription(description)
	 */
	public void createProject(String description) 
		throws IllegalDescriptionException,IllegalDuplicationException
	{
		this.pool.addProject(description);
	}
	
	
	/**
	 * Removes a project from the pool.
	 * 
	 * @param 	description
	 *  		The description of the project you want to remove from the pool.
	 * 
	 * @pre 	String exists in the project database. If it doesn't, we ignore the remove.
	 * 
	 * @post 	The project with his description = $description is removed from the pool.
	 */
	public void removeProject(String description)
	{
		this.pool.removeProject(description);
	}
	
	/**
	 * Adds a task to a project.
	 * 
	 * @param 	task
	 *  		The description of the task.
	 *  
	 * @param 	project
	 *  		The project to add the task to.
	 *  
	 * @post 	task is added to project.
	 * 
	 * from pool it throws
	 * 
	 * @throws 	IllegalTaskException
	 *  		if the task can not be found
	 *  		| isUniqueTask(task)
	 * 
	 * @throws 	IllegalAddTaskException
	 *  		if the task already has a project
	 *  		| task.getproject != null
	 *  
	 * @throws 	IllegalProjectException
	 *  		if the project cannot be found
	 */
	public void addTask(String project, String task) 
		throws IllegalTaskException,IllegalAddTaskException,IllegalProjectException
	{
		this.pool.addTask(project, task);
	}
	
	
	/**
	 * Returns the task list of a certain project.
	 * 
	 * @param 	projectDescription 
	 *  		The project (description) you want to see a task list of.
	 *  
	 * @return 	project.getTasks()
	 * 
	 * from the pool it throws :
	 * 
	 * @throws 	IllegalProjectException
	 *  		if the project can not be found
	 */
	public List<String> getTasks(String projectDescription) 
		throws IllegalProjectException
	{
		return pool.getTasks(projectDescription);
	}
	
	/**
	 * Reference to ProjectPool.getInstance().
	 * Just to avoid having to write that all the frakking time.
	 */
	private ProjectPool pool;
}